import { Component, OnInit } from '@angular/core';
import { MenuItem } from 'primeng/primeng';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { CommonService } from '../common/service/common.service';
import { switchMap } from 'rxjs/operators';
import { SYSTEM_TITLE } from "../common/configuration";

@Component({
    templateUrl: 'system.component.html',
    styleUrls: ['system.component.scss']
})
export class SystemComponent implements OnInit {
    sysId: number;
    subTitle: string;
    systemTitle: string;
    sysIcon: string;
    items: MenuItem[];
    constructor(
        private router: Router,
        private route: ActivatedRoute,
        private commonService: CommonService) {
    }
    /** 数据初始化 */
    public ngOnInit() {
        this.systemTitle = SYSTEM_TITLE;

        let afterUrl = decodeURI(window.location.pathname).substring(1);
        let strings = afterUrl.split('/');
        let url = strings[0];
        if (url != null || url != '') {
            this.commonService.getInitSystemByUrl(url).subscribe(data => {
                this.subTitle = data.sysName;
                this.sysIcon = '/assets/images/desktop/' + data.iconImage + '.png';
            });
        } else {
            this.commonService.getInitSystemByUrl("system").subscribe(data => {
                this.subTitle = data.sysName;
                this.sysIcon = '/assets/images/desktop/' + data.iconImage + '.png';
            });
        }

        this.route.paramMap.pipe(
            switchMap((params: ParamMap) => {
                this.sysId = +params.get('sysId') || 2;
                return this.commonService.initsystemresource(this.sysId)
            })
        ).subscribe(data => {
            this.items = data.menus;
            let resources = (localStorage.getItem('resources') || '').split(',');
            localStorage.setItem('resources', resources.concat(data.resIds).join(','));
        });
    }
}

